package com.app.business.calculate;

import com.app.business.en.HouseType;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.business.utils.AreaAndPriceUtils;
import com.app.business.utils.CalculateDate;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.ApplicationContextProvider;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.google.common.collect.Maps;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 惠晋江市英林镇英林村旧村改造（一期）回迁改造土地房屋征收补偿确认书相关计算
 * Created by szy on 2024/11/6
 */
public class CalculateSfUtils {
    private static ContractService contractService;
    private static OverAreaPriceNewService overAreaPriceNewService;
    private static OverAreaOneFloorSyPriceService overAreaOneFloorSyPriceService;
    private static OverAreaTwoFloorSyPriceService overAreaTwoFloorSyPriceService;
    private static OverAreaXzlPriceService overAreaXzlPriceService;
    private static OverAreaScPriceService overAreaScPriceService;
    private static SyKgInfoService syKgInfoService;
    private static HouseService houseService;
    private static StorageRoomService storageRoomService;
    private static CarLotService carLotService;
    private static DataDictService dataDictService;
    private static ShopHouseService shopHouseService;
    private static AdjustAreaService adjustAreaService;

    /**
     * 自动计算
     * 接受一个对象，对此对象的方案补助款分别计算
     *
     * @param contractId
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public synchronized static void main(Integer contractId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        contractService = (ContractService) applicationContext.getBean(ContractService.class);
        houseService = (HouseService) applicationContext.getBean(HouseService.class);
        storageRoomService = (StorageRoomService) applicationContext.getBean(StorageRoomService.class);
        carLotService = (CarLotService) applicationContext.getBean(CarLotService.class);
        dataDictService = (DataDictService) applicationContext.getBean(DataDictService.class);
        overAreaPriceNewService = (OverAreaPriceNewService) applicationContext.getBean(OverAreaPriceNewService.class);
        overAreaOneFloorSyPriceService = (OverAreaOneFloorSyPriceService) applicationContext.getBean(OverAreaOneFloorSyPriceService.class);
        overAreaTwoFloorSyPriceService = (OverAreaTwoFloorSyPriceService) applicationContext.getBean(OverAreaTwoFloorSyPriceService.class);
        overAreaXzlPriceService = (OverAreaXzlPriceService) applicationContext.getBean(OverAreaXzlPriceService.class);
        overAreaScPriceService = (OverAreaScPriceService) applicationContext.getBean(OverAreaScPriceService.class);
        syKgInfoService = (SyKgInfoService) applicationContext.getBean(SyKgInfoService.class);
        shopHouseService = (ShopHouseService) applicationContext.getBean(ShopHouseService.class);
        adjustAreaService = (AdjustAreaService) applicationContext.getBean(AdjustAreaService.class);
        Contract contract = contractService.findOne(contractId);
        if (contract == null || contract.getZsProject() == null) {
            return;
        }
        //总安置价款计算 （产权调换面积（不含照顾购买面积）-车位面积-回购面积）*3800+照顾购买面积*2300+扩购3800优惠面积*3800+扩购9100面积*9100）
        BigDecimal azJMoneyContract = BigDecimal.ZERO;
        BigDecimal houseMoney = contract.getHouseMoney().compareTo(BigDecimal.ZERO) > 0 ? BigDecimal.ZERO : contract.getHouseMoney().negate();
        //扩购金额
        azJMoneyContract = BigDecimalUtils.getSum(
                BigDecimalUtils.getBigAndBigMul(
                        BigDecimalUtils.getBigAndBigSub(
                                BigDecimalUtils.getBigAndBigSub(contract.getPropertyChangeArea(),contract.getCarLotTotalZhArea()),
                                contract.getZzResultArea().compareTo(BigDecimal.ZERO) < 0 ? contract.getZzResultArea().negate() : BigDecimal.ZERO
                        ),
                        new BigDecimal("3800")
                ),
                BigDecimalUtils.getBigAndBigMul(
                        contract.getCareBuyArea1(),
                        new BigDecimal("2300")
                ),
                houseMoney
        );
        contract.setAzJMoney(azJMoneyContract);
        contractService.save(contract);

        //安置款差额总计
        BigDecimal azkCeMoneyContract = BigDecimal.ZERO;

        BigDecimal resultMoney = contract.getResultMoney().compareTo(BigDecimal.ZERO) > 0 ? BigDecimal.ZERO : contract.getResultMoney();
        azkCeMoneyContract = BigDecimalUtils.getSum(contract.getHouseMoney(),resultMoney).compareTo(BigDecimal.ZERO) > 0 ?
                BigDecimal.ZERO : BigDecimalUtils.getSum(contract.getHouseMoney(),resultMoney).negate();

        List<House> houseList = houseService.getHouseListByContractId(contract.getId());
        int index = 0;
        BigDecimal noLastBl = BigDecimal.ZERO;//不含最后一套比例 最后一套比例核减
        BigDecimal noLastAzjMoney = BigDecimal.ZERO;//不含最后一套安置价款 最后一套安置价款核减
        BigDecimal noLastAzkCeMoney = BigDecimal.ZERO;//不含最后一套安置款差额 最后一套安置款差额核减
        if (CollectionUtils.isNotEmpty(houseList)) {
            for (House house : houseList) {
                index = index++;
                BigDecimal houseBl = BigDecimal.ZERO;//每套房面积占比
                BigDecimal azjMoney = BigDecimal.ZERO;//安置价款
                BigDecimal azkCeMoney = BigDecimal.ZERO;//安置款差额
                BigDecimal landHouseBcDkMoney = BigDecimal.ZERO;//土地房屋补偿款抵扣

                if (index != houseList.size()) {
                    //单套计算【首先计算比例】
                    houseBl = BigDecimalUtils.getBigAndBigDiv(house.getBuildArea(),contract.getChooseHouseAreaTotal(),8);//保留八位小数
                    azjMoney = BigDecimalUtils.getBigAndBigMul(houseBl,azJMoneyContract).setScale(2,BigDecimal.ROUND_HALF_UP);
                    azkCeMoney = BigDecimalUtils.getBigAndBigMul(houseBl,azkCeMoneyContract).setScale(2,BigDecimal.ROUND_HALF_UP);

                    //不含最后一条合计
                    noLastBl = BigDecimalUtils.getSum(noLastBl,houseBl);//不含最后一套比例计算
                    noLastAzjMoney = BigDecimalUtils.getSum(noLastAzjMoney,azjMoney);//不含最后一套安置价款
                    noLastAzkCeMoney = BigDecimalUtils.getSum(noLastAzkCeMoney,azkCeMoney);//不含最后一套安置差额

                } else {
                    houseBl = BigDecimalUtils.getBigAndBigSub(BigDecimal.ONE,noLastBl);//最后一条比例计算
                    azjMoney = BigDecimalUtils.getBigAndBigSub(azJMoneyContract,noLastAzjMoney);//最后一条安置价款
                    azkCeMoney = BigDecimalUtils.getBigAndBigSub(azkCeMoneyContract,noLastAzkCeMoney);//最后一条安置差额
                }
                landHouseBcDkMoney = BigDecimalUtils.getBigAndBigSub(
                        BigDecimalUtils.getBigAndBigDiv(azjMoney,new BigDecimal("1.09"),2),
                        azkCeMoney);

                house.setHouseAreaBl(houseBl);
                house.setAzjMoney(azjMoney);
                house.setAzkCeMoney(azkCeMoney);
                house.setLandHouseBcDkMoney(landHouseBcDkMoney);

                houseService.save(house);
            }
        }

        //车位安置价款计算
        List<CarLot> carLotList = carLotService.getCarLotListByContractId(contract.getId());
        if (CollectionUtils.isNotEmpty(carLotList)) {
            for (CarLot carLot : carLotList) {
                BigDecimal azjMoney = BigDecimal.ZERO;//安置价款
                BigDecimal azkCeMoney = BigDecimal.ZERO;//安置款差额
                BigDecimal landHouseBcDkMoney = BigDecimal.ZERO;//土地房屋补偿款抵扣
                if (carLot.getSfHg() == 1) {//否 是否换购
                    azjMoney = carLot.getZzXjnPrice();//即车位最终需缴纳金额
                    landHouseBcDkMoney = BigDecimal.ZERO;
                    azkCeMoney = BigDecimalUtils.getBigAndBigDiv(azjMoney,new BigDecimal(1.09),2);
                } else {//是 是否换购 用18平方米换购
                    azjMoney = BigDecimalUtils.getSum(
                            BigDecimalUtils.getBigAndBigMul(new BigDecimal("18"),new BigDecimal("3800")),
                            carLot.getZzXjnPrice()
                    );
                    landHouseBcDkMoney = BigDecimalUtils.getBigAndBigSub(
                            BigDecimalUtils.getBigAndBigDiv(azjMoney,new BigDecimal(1.09),2),
                            azkCeMoney);
                    azkCeMoney = BigDecimal.ZERO;
                }

                carLot.setAzjMoney(azjMoney);
                carLot.setAzkCeMoney(azkCeMoney);
                carLot.setLandHouseBcDkMoney(landHouseBcDkMoney);

                carLotService.save(carLot);
            }
        }

        //店面安置价款计算
        List<ShopHouse> shopHouseList = shopHouseService.getShopHouseListByContractId(contract.getId());
        BigDecimal oneFloorBuildArea = BigDecimal.ZERO,twoFloorBuildArea = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(shopHouseList)) {
            for (ShopHouse shopHouse : shopHouseList) {
                oneFloorBuildArea = BigDecimalUtils.getSum(oneFloorBuildArea,shopHouse.getOneFloorArea());
                twoFloorBuildArea = BigDecimalUtils.getSum(twoFloorBuildArea,shopHouse.getTwoFloorArea());
            }
        }

        if (CollectionUtils.isNotEmpty(shopHouseList)) {
            for (ShopHouse shopHouse : shopHouseList) {
                BigDecimal houseBl = BigDecimal.ZERO;//每套房面积占比
                BigDecimal oneFloorBl = BigDecimal.ZERO;//一层比例
                BigDecimal twoFloorBl = BigDecimal.ZERO;//二层比例
                BigDecimal azjMoney = BigDecimal.ZERO;//安置价款
                BigDecimal azkCeMoney = BigDecimal.ZERO;//安置款差额
                BigDecimal landHouseBcDkMoney = BigDecimal.ZERO;//土地房屋补偿款抵扣
                oneFloorBl = BigDecimalUtils.getBigAndBigDiv(shopHouse.getOneFloorArea(),oneFloorBuildArea,8);//保留八位小数
                twoFloorBl = BigDecimalUtils.getBigAndBigDiv(shopHouse.getTwoFloorArea(),twoFloorBuildArea,8);//保留八位小数
                houseBl = BigDecimalUtils.getBigAndBigDiv(shopHouse.getBuildArea(),contract.getShopHouseArea(),8);//保留八位小数
                BigDecimal zAzArea = BigDecimalUtils.getSum(
                        contract.getSyDmAzArea(),
                        contract.getSyTwoFloorAzArea(),
                        contract.getSyXzlAzArea(),
                        contract.getSyXzlAzArea(),
                        contract.getSyScAzArea()
                );
                azjMoney = BigDecimalUtils.getSum(
                        BigDecimalUtils.getBigAndBigMul(oneFloorBl,BigDecimalUtils.getBigAndBigMul(zAzArea,contract.getOneFloorSyPrice())).setScale(2,BigDecimal.ROUND_HALF_UP),
                        BigDecimalUtils.getBigAndBigMul(twoFloorBl,BigDecimalUtils.getBigAndBigMul(zAzArea,contract.getTwoFloorSyPrice())).setScale(2,BigDecimal.ROUND_HALF_UP)
                );
                azjMoney = BigDecimalUtils.getSum(azjMoney,BigDecimalUtils.getBigAndBigMul(houseBl,azkCeMoneyContract).setScale(2,BigDecimal.ROUND_HALF_UP));


                azkCeMoney = BigDecimalUtils.getBigAndBigMul(houseBl,azkCeMoneyContract).setScale(2,BigDecimal.ROUND_HALF_UP);
                landHouseBcDkMoney = BigDecimalUtils.getBigAndBigSub(
                        BigDecimalUtils.getBigAndBigDiv(azjMoney,new BigDecimal(1.09),2),
                        azkCeMoney);
                shopHouse.setHouseAreaBl(houseBl);
                shopHouse.setAzjMoney(azjMoney);
                shopHouse.setAzkCeMoney(azkCeMoney);
                shopHouse.setLandHouseBcDkMoney(landHouseBcDkMoney);
                shopHouseService.save(shopHouse);
            }
        }
        System.out.println("************************土地房屋征收补偿确认书计算完成！************************");
    }

}
